<?php

include('common.php');

// get variable ID from GET
$name = $G['name'];
$delete_flag = $G['delete'];
$new_name = $G['new_name'];
$table = $G['table']; // if this is a table row, this has the table's name
$subject = (empty($table)) ? "Variable" : "Table Column";

// delete if there is delete flag
if (!empty($delete_flag))
{
	// delete this variable
	$user = user();
	if (!empty($table))
	{
		sql_exec("DELETE FROM Variables WHERE User_Table='$table' AND User='$user' AND Name='$name'");
	} else {
		sql_exec("DELETE FROM Variables WHERE User='$user' AND Name='$name'");
	}
	
	// return to memory
	header("Location: memory.php?domain=$domain");
}

// catch form return, if there is value, store it and return to memory
if (!empty($new_name))
{
	// store it
	$user = user();
	if (!empty($table))
	{
		sql_exec("UPDATE Variables SET Name='$new_name' WHERE User_Table='$table' AND User='$user' AND Name='$name'");
	} else {
		sql_exec("UPDATE Variables SET Name='$new_name' WHERE User='$user' AND Name='$name'");
	}

	// return to memory
	header("Location: memory.php?domain=$domain");
}



html_header("edit variable");
?>

<body>

<div id="container">

<?php header_div(); ?>


<div id="body">

<h1>edit <?php echo $subject; ?></h1>



<p>
<form action="edit_variable.php" method="get">
<p><?php echo $subject; ?> Name: <input name="new_name" value="<?php echo $name; ?>" /></p>
<input type="hidden" name="domain" value="<?php echo $domain; ?>" />
<input type="hidden" name="table" value="<?php echo $table; ?>" />
<input type="hidden" name="name" value="<?php echo $name; ?>" />
<input type="submit" value="rename" />
</form>
</p>

<p>
<form action="edit_variable.php" method="get">
<input type="hidden" name="delete" value="delete" />
<input type="hidden" name="domain" value="<?php echo $domain; ?>" />
<input type="hidden" name="table" value="<?php echo $table; ?>" />
<input type="hidden" name="name" value="<?php echo $name; ?>" />
<input type="submit" value="delete" />
</form>

</p>

</div>

<?php footer(); ?>

</div>
</body>
</html>
